import request from '../utils/request'

export function addCart(bookData) {
  return request({
    url: '/cart/add',
    method: 'post',
    params: {
      bookId: bookData.bookId,
      quantity: bookData.quantity || 1
    }

  })
}

export const fetchCartList = (userId) => {
  return request({
    url: `/cart/list/${userId}`,
    method: 'get'
  })
}

export const updateCartItemByCartId = (cartId, quantity) => {
  return request({
    url: `/cart/updateQuantity`,
    method: 'post',
    params: {
      bookId: cartId,
      quantity: quantity
    }
  })
}


export const removeCartItemByCartId = (cartId) => {
  return request({
    url: `/cart/remove/${cartId}`,
    method: 'DELETE',

  })
}

export const removeCartItemByCartIdList = (cartIdList) => {
  const params = new URLSearchParams();
  // 遍历数组，逐个添加参数
  cartIdList.forEach(item => {
    params.append('cartIds', item);
  });
  return request({
    url: `/cart/remove`,
    method: 'DELETE',
    // 直接传拼接好的 params
    params: params
  });
}